from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import viewsets, status
from rest_framework.decorators import action
from rest_framework.filters import OrderingFilter
from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response
from apps.club_system.club.serializers.accouny_role import AccountRolesSerializer
from apps.common.account.models import AccountRoles, Role
from utils.common import Pagination


class AccountRolesViewSet(ModelViewSet):
    queryset = AccountRoles.objects.all()
    filter_backends = (OrderingFilter, DjangoFilterBackend)
    pagination_class = Pagination
    ordering_fields = ('id', 'created_time')
    # filter_class = ClubFilter
    serializer_class = AccountRolesSerializer
    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()
        self.perform_destroy(instance)
        return Response({'message': '删除成功'}, status=status.HTTP_204_NO_CONTENT)

    @action(methods=['get'], detail=False, url_path='get_role')
    def get_role(self, request):
        queryset = Role.objects.all().values('id', 'name').order_by('id')
        return Response(queryset)
